<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 平台测试管理
 */
class Plataward extends Admin_Controller {
	
    function __construct()
    {
        parent::__construct();
        $this->load->model('plat_model');
        $this->load->model('plat_cpa_model');
        $this->load->model('users_model');
        $this->load->model('plat_invest_model');
        $this->load->model('plat_award_list_model');
    }
    
    public function index()
    {
//        $my_excel = new Mexcel();
//        $filename = BASEPATH.'..'.'/data/upfiles/excel/20160323/1458734398998.xls';
//        $result = $my_excel->excel_to_array($filename);
        $this->template->load('plataward/index');
    }
   //富沧金融
    public function fucangvip(){
        $this->template->assign('plat_en', 'fucangvip');
        $this->template->load('plataward/fucangvip');
    }
    //导入数据
    public function import_fucangvip_award(){
        $mess = array();
        if($_POST){
            $my_excel = new Mexcel();
            $file_name = trim($_POST['file_url']);
            $name_en = trim($_POST['name_en']);
            $result = $my_excel->excel_to_array($file_name);
            if($result['status'] == 1 ){
                $list_ = $result['data'];
                if(!$list_){
                    $mess['status'] = 1;
                    $mess['error'] = '没有发现任何数据';
                    echo json_encode($mess);die;
                }
                $plat = $this->plat_model->get(array('name_en'=>$name_en,'is_show'=>1));
                if(!$plat){
                    $mess['status'] = 1;
                    $mess['error'] = '平台不存在或已下线';
                    echo json_encode($mess);die;
                }
                $rules = $this->plat_model->get_plat_rules(array('plat_id'=>$plat['id'],'is_show'=>1));
                $list = array();
                $flag_name = 0;//判断平台名称是否正确
                $flag_phone = 0;//判断手机号格式是否正确
                foreach ($list_ as $key=>$val){
                    if($val[0] != $plat['name_ch']){
                        $flag_name = 1;
                        break;
                    }
                    if(!preg_match("/^1[3|4|5|7|8]\d{9}$/", $val[2])){
                        $flag_phone = 1;
                        break;
                    }

                    $temp = array();
                    $temp['plat_id'] = $plat['id'];
                    $temp['name_ch'] = $val[0];
                    $temp['usernamep'] = $val[1];
                    $temp['mobile'] = $val[2];
                    $temp['is_data_bind'] = $val[3] == '是' ? 1 : 0;
                    $temp['reg_time'] = strtotime($val[4]);
                    $temp['order_nid'] = $val[5];
                    $temp['borrow_nid'] = $val[6];
                    $temp['title'] = $val[7];
                    $temp['url'] = $val[8];
                    $temp['amount'] = $val[9];
                    $temp['period'] = $val[10];
                    $temp['unit'] = $val[11] == '月' ? 2 : 1;
                    $temp['rate'] = $val[12];
                    $temp['invest_time'] = strtotime($val[13]);
                    $list[] = $temp;
                }
                if($flag_name){
                    $mess['status'] = 1;
                    $mess['error'] = '平台名称解析错误';
                    echo json_encode($mess);die;
                }
                if($flag_phone){
                    $mess['status'] = 1;
                    $mess['error'] = '手机号码格式解析错误';
                    echo json_encode($mess);die;
                }
                
                $import_success = 0;//成功导入
                $import_fail = 0;//失败导入
                $import_success_str = '';//成功导入
                $import_fail_str = '';//失败导入
                foreach ($list as $key=>$val){
                    $user = $this->users_model->get_user_by_mobile($val['mobile']);
                    if($user){
                        $cpa = $this->plat_cpa_model->get_ab_user_info(array('user_id'=>$user['user_id'],'plat_id'=>$plat['id']));
                        //如果已经绑定cpa
                        if($cpa){
                            
                            if(!$cpa['is_finish']){
                                //cpa绑定信息验证
                                if(!$cpa['is_data_bind'] && $val['is_data_bind']){
                                    $cpa_data = array();
                                    $cpa_data['is_data_bind'] = 1;
                                    $status = $this->plat_cpa_model->update_ab($cpa_data,$cpa['id']);
                                    if($status){
                                        $rule_one = $rules[0];   
                                        $data = array();
                                        $data['user_id'] = $user['user_id'];
                                        $data['plat_id'] = $plat['id'];
                                        $data['rule_step'] = 1;
                                        $data['award'] = $rule_one['award'];
                                        $data['exper'] = $rule_one['exper'];
                                        $data['desc'] = $rule_one['rule'];
                                        $data['create_time'] = time();
                                        $this->plat_award_list_model->insert($data);
                                        $plat = $this->plat_model->get(array('name_en'=>$name_en));
                                        //更新平台信息
                                        $plat_data = array();
                                        $plat_data['pay_amount'] = $plat['pay_amount'] - $rule_one['true_amount'];
                                        if($plat_data['pay_amount'] <= 20000000){
                                            $plat_data['is_show'] = 0;
                                        }
                                        $this->plat_model->update_plat($plat_data,$plat['id']);
                                        //添加平台体验日志
                                        $plat_log = array();
                                        $plat_log['user_id'] = $user['user_id'];;
                                        $plat_log['plat_id'] = $plat['id'];
                                        $plat_log['plat_balance'] = $plat_data['pay_amount'];
                                        $plat_log['award'] = $data['award'];
                                        $plat_log['true_award'] = $rule_one['true_amount'];
                                        $plat_log['experience'] = $data['exper'];
                                        $plat_log['create_time'] = time();
                                        $this->plat_model->insert_plat_log($plat_log);

                                        //发送奖励通知
                                        $message = "您已经完成【".$plat['name_ch'].'】平台第1步【'.$rule_one['rule']."】的体验,将获得<span class='mail-award'>".$rule_one['award']."溜溜币</span>和<span class='mail-award'>".$rule_one['exper']."积分</span>,赶快去<a href='/mplat_fucangvip.html' target='_blank' class='mail-award-a'>立即领取&gt;&gt;</a>吧。感谢您对弹溜溜的支持！";
                                        $message = mail_notice_tpl($this->config_tpl['mail_tpl'], $message);
                                        $this->db->insert("users_award_notice", array('user_id'=>$user['user_id'],'title'=>'奖励通知','content'=>$message,'from'=>'弹溜溜','create_time'=>  time()));
                                    }
                                }
                                
                                //首投信息验证
                                if(!$cpa['amount'] && $val['amount']){
                                    $plat = $this->plat_model->get(array('name_en'=>$name_en));
                                    $amount = intval($val['amount']);
                                    foreach ($rules as $k=>$rule){
                                        if($k > 0){//
                                            $step_index = $rule['step'];
                                            $min_amount = $rule['min_amount'];
                                            $max_amount = $rule['max_amount'];
                                            if($amount >= $min_amount && $amount <= $max_amount){//符合要求
                                                $true_amount = $rule['true_amount'];//实际扣除金额
                                                $award_amount = $rule['award'];//奖励金额
                                                $exper_amount = $rule['exper'];//奖励经验
                                                //增加奖励记录
                                                $award_list = array();
                                                $award_list['user_id'] = $user['user_id'];
                                                $award_list['plat_id'] = $plat['id'];
                                                $award_list['rule_step'] = $step_index;
                                                $award_list['award'] = $award_amount;
                                                $award_list['exper'] = $exper_amount;
                                                $award_list['desc'] = '首次产品体验金额'.  sprintf('%0.2f',$amount).'元';
                                                $award_list['create_time'] = time();
                                                $invest_result = $this->plat_award_list_model->insert($award_list);
                                                if($invest_result){//添加成功
                                                    //更新绑定信息
                                                    $cpa_data = array();
                                                    $cpa_data['amount'] = $amount;
                                                    $cpa_data['is_finish'] = 1;
                                                    $this->plat_cpa_model->update_ab($cpa_data,$cpa['id']);
                                                    //更新平台信息
                                                    $plat_data = array();
                                                    $plat_data['pay_amount'] = $plat['pay_amount'] - $true_amount;
                                                    if($plat_data['pay_amount'] <= 20000000){
                                                        $plat_data['is_show'] = 0;
                                                    }
                                                    $this->plat_model->update_plat($plat_data,$plat['id']);
                                                    //添加平台体验日志
                                                    $plat_log = array();
                                                    $plat_log['user_id'] = $user['user_id'];
                                                    $plat_log['plat_id'] = $plat['id'];
                                                    $plat_log['plat_balance'] = $plat_data['pay_amount'];
                                                    $plat_log['award'] = $award_amount;
                                                    $plat_log['true_award'] = $true_amount;
                                                    $plat_log['experience'] = $exper_amount;
                                                    $plat_log['create_time'] = time();
                                                    $this->plat_model->insert_plat_log($plat_log);
                                                }
                                                break;
                                            }
                                        }
                                    }
                                    $where = array();
                                    $where['user_id'] = $user['user_id'];
                                    $where['plat_id'] = $plat['id'];
                                    $where['order_nid'] = $val['order_nid'];
                                    $invest = $this->plat_invest_model->get_plat_invest($where);
                                    //投标订单不存在
                                    if(!$invest){
                                        $invest_data = array();
                                        $invest_data['user_id'] = $user['user_id'];
                                        $invest_data['plat_id'] = $plat['id'];
                                        $invest_data['order_nid'] = $val['order_nid'];
                                        $invest_data['borrow_nid'] = $val['borrow_nid'];
                                        $invest_data['title'] = $val['title'];
                                        $invest_data['url'] = $val['url'];
                                        $invest_data['amount'] = $val['amount'];
                                        $invest_data['invest_time'] = $val['invest_time'];
                                        $invest_data['period'] = $val['period'];
                                        $invest_data['unit'] = $val['unit'];
                                        $invest_data['rate'] = $val['rate'];
                                        $invest_data['create_time'] = time();
                                        $invest_id = $this->plat_invest_model->insert($invest_data);
                                    }
                                }
                            }
                            
                        }else{//如果未绑定cpa
                            
                            //构造cpa数据
                            $cpa_data = array();
                            $cpa_data['user_id'] = $user['user_id'];
                            $cpa_data['plat_id'] = $plat['id'];
                            $cpa_data['usernamep'] = $val['usernamep'];
                            //判断是否完成cpa信息绑定
                            if($val['is_data_bind']){
                                $cpa_data['is_data_bind'] = 1;
                                $rule_one = $rules[0];   
                                $data = array();
                                $data['user_id'] = $user['user_id'];
                                $data['plat_id'] = $plat['id'];
                                $data['rule_step'] = 1;
                                $data['award'] = $rule_one['award'];
                                $data['exper'] = $rule_one['exper'];
                                $data['desc'] = $rule_one['rule'];
                                $data['create_time'] = time();
                                $this->plat_award_list_model->insert($data);
                                $plat = $this->plat_model->get(array('name_en'=>$name_en));
                                //更新平台信息
                                $plat_data = array();
                                $plat_data['pay_amount'] = $plat['pay_amount'] - $rule_one['true_amount'];
                                if($plat_data['pay_amount'] <= 20000000){
                                    $plat_data['is_show'] = 0;
                                }
                                $this->plat_model->update_plat($plat_data,$plat['id']);
                                //添加平台体验日志
                                $plat_log = array();
                                $plat_log['user_id'] = $user['user_id'];;
                                $plat_log['plat_id'] = $plat['id'];
                                $plat_log['plat_balance'] = $plat_data['pay_amount'];
                                $plat_log['award'] = $data['award'];
                                $plat_log['true_award'] = $rule_one['true_amount'];
                                $plat_log['experience'] = $data['exper'];
                                $plat_log['create_time'] = time();
                                $this->plat_model->insert_plat_log($plat_log);
                                
                                //发送奖励通知
                                $message = "您已经完成【".$plat['name_ch'].'】平台第1步【'.$rule_one['rule']."】的体验,将获得<span class='mail-award'>".$rule_one['award']."溜溜币</span>和<span class='mail-award'>".$rule_one['exper']."积分</span>,赶快去<a href='/mplat_fucangvip.html' target='_blank' class='mail-award-a'>立即领取&gt;&gt;</a>吧。感谢您对弹溜溜的支持！";
                                $message = mail_notice_tpl($this->config_tpl['mail_tpl'], $message);
                                $this->db->insert("users_award_notice", array('user_id'=>$user['user_id'],'title'=>'奖励通知','content'=>$message,'from'=>'弹溜溜','create_time'=>  time()));
                                
                            }else{
                                $cpa_data['is_data_bind'] = 0;
                            }
                            //判断是否完成首投
                            if($val['amount']){
                                
                                $plat = $this->plat_model->get(array('name_en'=>$name_en));
                                $cpa_data['amount'] = sprintf('%0.2f',$val['amount']);
                                $amount = intval($val['amount']);
                                foreach ($rules as $k=>$rule){
                                    if($k > 0){//
                                        $step_index = $rule['step'];
                                        $min_amount = $rule['min_amount'];
                                        $max_amount = $rule['max_amount'];
                                        if($amount >= $min_amount && $amount <= $max_amount){//符合要求
                                            $true_amount = $rule['true_amount'];//实际扣除金额
                                            $award_amount = $rule['award'];//奖励金额
                                            $exper_amount = $rule['exper'];//奖励经验
                                            //增加奖励记录
                                            $award_list = array();
                                            $award_list['user_id'] = $user['user_id'];
                                            $award_list['plat_id'] = $plat['id'];
                                            $award_list['rule_step'] = $step_index;
                                            $award_list['award'] = $award_amount;
                                            $award_list['exper'] = $exper_amount;
                                            $award_list['desc'] = '首次产品体验金额'.  sprintf('%0.2f',$amount).'元';
                                            $award_list['create_time'] = time();
                                            $invest_result = $this->plat_award_list_model->insert($award_list);
                                            if($invest_result){//添加成功
                                                //更新平台信息
                                                $plat_data = array();
                                                $plat_data['pay_amount'] = $plat['pay_amount'] - $true_amount;
                                                if($plat_data['pay_amount'] <= 20000000){
                                                    $plat_data['is_show'] = 0;
                                                }
                                                $this->plat_model->update_plat($plat_data,$plat['id']);
                                                //添加平台体验日志
                                                $plat_log = array();
                                                $plat_log['user_id'] = $user['user_id'];
                                                $plat_log['plat_id'] = $plat['id'];
                                                $plat_log['plat_balance'] = $plat_data['pay_amount'];
                                                $plat_log['award'] = $award_amount;
                                                $plat_log['true_award'] = $true_amount;
                                                $plat_log['experience'] = $exper_amount;
                                                $plat_log['create_time'] = time();
                                                $this->plat_model->insert_plat_log($plat_log);
                                                
                                                //发送奖励通知
                                                $message = "您已经完成【".$plat['name_ch'].'】平台第'.$step_index.'步【'.$rule['rule']."】的体验,将获得<span class='mail-award'>".$award_amount."溜溜币</span>和<span class='mail-award'>".$exper_amount."积分</span>,赶快去<a href='/mplat_fucangvip.html' target='_blank' class='mail-award-a'>立即领取&gt;&gt;</a>吧。感谢您对弹溜溜的支持！";
                                                $message = mail_notice_tpl($this->config_tpl['mail_tpl'], $message);
                                                $this->db->insert("users_award_notice", array('user_id'=>$user['user_id'],'title'=>'奖励通知','content'=>$message,'from'=>'弹溜溜','create_time'=>  time()));
                                                
                                            }
                                            break;
                                        }
                                    }
                                }
                                
                                $where = array();
                                $where['user_id'] = $user['user_id'];
                                $where['plat_id'] = $plat['id'];
                                $where['order_nid'] = $val['order_nid'];
                                $invest = $this->plat_invest_model->get_plat_invest($where);
                                //投标订单不存在
                                if(!$invest){
                                    $invest_data = array();
                                    $invest_data['user_id'] = $user['user_id'];
                                    $invest_data['plat_id'] = $plat['id'];
                                    $invest_data['order_nid'] = $val['order_nid'];
                                    $invest_data['borrow_nid'] = $val['borrow_nid'];
                                    $invest_data['title'] = $val['title'];
                                    $invest_data['url'] = $val['url'];
                                    $invest_data['amount'] = $val['amount'];
                                    $invest_data['invest_time'] = $val['invest_time'];
                                    $invest_data['period'] = $val['period'];
                                    $invest_data['unit'] = $val['unit'];
                                    $invest_data['rate'] = $val['rate'];
                                    $invest_data['create_time'] = time();
                                    $invest_id = $this->plat_invest_model->insert_plat_invest_list($invest_data);
                                }
                                
                            }else{
                                $cpa_data['amount'] = 0;
                            }
                            if($cpa_data['is_data_bind'] && $cpa_data['amount']){
                                $cpa_data['is_finish'] = 1; 
                            }
                            $cpa_data['reg_time'] = $val['reg_time'];
                            //插入绑定信息
                            $this->plat_cpa_model->insert_ab($cpa_data);
                            
                        }
                        $import_success++;
                        $import_success_str .= $val['mobile'].',';
                    }else{
                        $import_fail++;
                        $import_fail_str .= $val['mobile'].',';
                    }
                }
                $mess['status'] = 0;
                $mess['info'] = '成功导入'.$import_success.'条数据['.$import_success_str.'],失败导入'.$import_fail.'条数据['.$import_fail_str.']';
            }else{
                $mess['status'] = 1;
                $mess['error'] = $result['error'];
            }
            
            echo json_encode($mess);die;
        }
    }
    //理财范
    public function licaifan(){
        $this->template->assign('plat_en', 'licaifan');
        $this->template->load('plataward/licaifan');
    }
    //导入理财范数据
    public function import_licaifan_award(){
        $mess = array();
        if($_POST){
            $my_excel = new Mexcel();
            $file_name = trim($_POST['file_url']);
            $name_en = trim($_POST['name_en']);
            $result = $my_excel->excel_to_array($file_name);
            if($result['status'] == 1 ){
                $list_ = $result['data'];
                if(!$list_){
                    $mess['status'] = 1;
                    $mess['error'] = '没有发现任何数据';
                    echo json_encode($mess);die;
                }
                $plat = $this->plat_model->get(array('name_en'=>$name_en,'is_show'=>1));
                if(!$plat){
                    $mess['status'] = 1;
                    $mess['error'] = '平台不存在或已下线';
                    echo json_encode($mess);die;
                }
                $rules = $this->plat_model->get_plat_rules(array('plat_id'=>$plat['id'],'is_show'=>1));
                $list = array();
                $flag_name = 0;//判断平台名称是否正确
                foreach ($list_ as $key=>$val){
                    if($val[0] != $plat['name_ch']){
                        $flag_name = 1;
                        break;
                    }
                    $temp = array();
                    $temp['plat_id'] = $plat['id'];
                    $temp['name_ch'] = $val[0];
                    $temp['mobile_first'] = $val[1];//手机前三位
                    $temp['mobile_end'] = $val[2];//手机后三位
                    $temp['reg_time'] = strtotime($val[3]);
                    $temp['borrow_nid'] = $val[4];
                    $temp['title'] = $val[5];
                    $temp['url'] = $val[6];
                    $temp['amount'] = $val[7];
                    $temp['period'] = $val[8];
                    $temp['unit'] = $val[9] == '月' ? 2 : 1;
                    $temp['rate'] = $val[10];
                    $temp['invest_time'] = strtotime($val[11]);
                    $list[] = $temp;
                }
                if($flag_name){
                    $mess['status'] = 1;
                    $mess['error'] = '平台名称解析错误';
                    echo json_encode($mess);die;
                }
                $import_success = 0;//成功导入
                $import_fail = 0;//失败导入
                $import_success_str = '';//成功导入
                $import_fail_str = '';//失败导入
                foreach ($list as $key=>$val){
                    $user = $this->plat_cpa_model->get_users_by_mobile_3_to_3($val['mobile_first'],$val['mobile_end'],$plat['name_en']);
                    if(!$user){
                        $user = $this->users_model->get_users_by_mobile_3_to_3($val['mobile_first'],$val['mobile_end']);
                    }
                    if($user){
                        $cpa = $this->plat_cpa_model->get_ab_user_info(array('user_id'=>$user['user_id'],'plat_en'=>$plat['name_en']));
                        //如果已经绑定cpa
                        if($cpa){
                            if(!$cpa['is_finish']){
                                //首投信息验证
                                if(!intval($cpa['amount']) && $val['amount']){
                                    $plat = $this->plat_model->get(array('name_en'=>$name_en,'is_show'=>1));
                                    $amount = intval($val['amount']);
                                    foreach ($rules as $k=>$rule){
                                            $step_index = $rule['step'];
                                            $min_amount = $rule['min_amount'];
                                            $max_amount = $rule['max_amount'];
                                            if($amount >= $min_amount && $amount <= $max_amount){//符合要求
                                                $true_amount = $rule['true_amount'];//实际扣除金额
                                                $award_amount = $rule['award'];//奖励金额
                                                $exper_amount = $rule['exper'];//奖励经验
                                                //增加奖励记录
                                                $award_list = array();
                                                $award_list['user_id'] = $user['user_id'];
                                                $award_list['plat_id'] = $plat['id'];
                                                $award_list['plat_en'] = $plat['name_en'];
                                                $award_list['rule_step'] = $step_index;
                                                $award_list['award'] = $award_amount;
                                                $award_list['exper'] = $exper_amount;
                                                $award_list['desc'] = '首次产品体验金额'.  sprintf('%0.2f',$amount).'元';
                                                $award_list['create_time'] = time();
                                                $invest_result = $this->plat_award_list_model->insert($award_list);
                                                if($invest_result){//添加成功
                                                    //更新绑定信息
                                                    $cpa_data = array();
                                                    $cpa_data['amount'] = $amount;
                                                    $cpa_data['is_finish'] = 1;
                                                    $cpa_data['is_data_bind'] = 1;
                                                    $this->plat_cpa_model->update_ab($cpa_data,$cpa['id']);
                                                    //更新平台信息
                                                    $plat_data = array();
                                                    $plat_data['pay_amount'] = $plat['pay_amount'] - $true_amount;
                                                    if($plat_data['pay_amount'] <= 500000){
                                                        $plat_data['is_show'] = 0;
                                                    }
                                                    $this->plat_model->update_plat($plat_data,$plat['id']);
                                                    //添加平台体验日志
                                                    $plat_log = array();
                                                    $plat_log['user_id'] = $user['user_id'];
                                                    $plat_log['plat_id'] = $plat['id'];
                                                    $plat_log['plat_en'] = $plat['name_en'];
                                                    $plat_log['plat_balance'] = $plat_data['pay_amount'];
                                                    $plat_log['award'] = $award_amount;
                                                    $plat_log['true_award'] = $true_amount;
                                                    $plat_log['experience'] = $exper_amount;
                                                    $plat_log['create_time'] = time();
                                                    $this->plat_model->insert_plat_log($plat_log);
                                                }
                                                break;
                                            }
                                    }
                                    $invest_data = array();
                                    $invest_data['user_id'] = $user['user_id'];
                                    $invest_data['plat_id'] = $plat['id'];
                                    $invest_data['plat_en'] = $plat['name_en'];
                                    $invest_data['order_nid'] = '';
                                    $invest_data['borrow_nid'] = $val['borrow_nid'];
                                    $invest_data['title'] = $val['title'];
                                    $invest_data['url'] = $val['url'];
                                    $invest_data['amount'] = $val['amount'];
                                    $invest_data['invest_time'] = $val['invest_time'];
                                    $invest_data['period'] = $val['period'];
                                    $invest_data['unit'] = $val['unit'];
                                    $invest_data['rate'] = $val['rate'];
                                    $invest_data['create_time'] = time();
                                    $invest_id = $this->plat_invest_model->insert($invest_data);
                                }
                            }
                            
                        }else{//如果未绑定cpa
                            
                            //构造cpa数据
                            $cpa_data = array();
                            $cpa_data['user_id'] = $user['user_id'];
                            $cpa_data['plat_id'] = $plat['id'];
                            $cpa_data['plat_en'] = $plat['name_en'];
                            $cpa_data['usernamep'] = $user['mobile'];
                            $cpa_data['is_data_bind'] = 0;
                            //判断是否完成首投
                            if($val['amount']){
                                $plat = $this->plat_model->get(array('name_en'=>$name_en,'is_show'=>1));
                                $cpa_data['amount'] = sprintf('%0.2f',$val['amount']);
                                $amount = intval($val['amount']);
                                foreach ($rules as $k=>$rule){
                                    $step_index = $rule['step'];
                                    $min_amount = $rule['min_amount'];
                                    $max_amount = $rule['max_amount'];
                                    if($amount >= $min_amount && $amount <= $max_amount){//符合要求
                                        $true_amount = $rule['true_amount'];//实际扣除金额
                                        $award_amount = $rule['award'];//奖励金额
                                        $exper_amount = $rule['exper'];//奖励经验
                                        //增加奖励记录
                                        $award_list = array();
                                        $award_list['user_id'] = $user['user_id'];
                                        $award_list['plat_id'] = $plat['id'];
                                        $award_list['plat_en'] = $plat['name_en'];
                                        $award_list['rule_step'] = $step_index;
                                        $award_list['award'] = $award_amount;
                                        $award_list['exper'] = $exper_amount;
                                        $award_list['desc'] = '首次产品体验金额'.  sprintf('%0.2f',$amount).'元';
                                        $award_list['create_time'] = time();
                                        $invest_result = $this->plat_award_list_model->insert($award_list);
                                        if($invest_result){//添加成功
                                            $cpa_data['is_data_bind'] = 1;
                                            //更新平台信息
                                            $plat_data = array();
                                            $plat_data['pay_amount'] = $plat['pay_amount'] - $true_amount;
                                            if($plat_data['pay_amount'] <= 500000){
                                                $plat_data['is_show'] = 0;
                                            }
                                            $this->plat_model->update_plat($plat_data,$plat['id']);
                                            //添加平台体验日志
                                            $plat_log = array();
                                            $plat_log['user_id'] = $user['user_id'];
                                            $plat_log['plat_id'] = $plat['id'];
                                            $plat_log['plat_en'] = $plat['name_en'];
                                            $plat_log['plat_balance'] = $plat_data['pay_amount'];
                                            $plat_log['award'] = $award_amount;
                                            $plat_log['true_award'] = $true_amount;
                                            $plat_log['experience'] = $exper_amount;
                                            $plat_log['create_time'] = time();
                                            $this->plat_model->insert_plat_log($plat_log);

                                            //发送奖励通知
                                            $message = "您已经完成【".$plat['name_ch'].'】平台第'.$step_index.'步【'.$rule['rule']."】的体验,将获得<span class='mail-award'>".$award_amount."溜溜币</span>和<span class='mail-award'>".$exper_amount."积分</span>,赶快去<a href='/user/try_list.html' target='_blank' class='mail-award-a'>立即领取&gt;&gt;</a>吧。感谢您对弹溜溜的支持！";
                                            $message = mail_notice_tpl($this->config_tpl['mail_tpl'], $message);
                                            $this->db->insert("users_award_notice", array('user_id'=>$user['user_id'],'title'=>'奖励通知','content'=>$message,'from'=>'弹溜溜','create_time'=>  time()));

                                        }
                                        break;
                                    }
                                }
                                $invest_data = array();
                                $invest_data['user_id'] = $user['user_id'];
                                $invest_data['plat_id'] = $plat['id'];
                                $invest_data['plat_en'] = $plat['name_en'];
                                $invest_data['order_nid'] = '';
                                $invest_data['borrow_nid'] = $val['borrow_nid'];
                                $invest_data['title'] = $val['title'];
                                $invest_data['url'] = $val['url'];
                                $invest_data['amount'] = $val['amount'];
                                $invest_data['invest_time'] = $val['invest_time'];
                                $invest_data['period'] = $val['period'];
                                $invest_data['unit'] = $val['unit'];
                                $invest_data['rate'] = $val['rate'];
                                $invest_data['create_time'] = time();
                                $invest_id = $this->plat_invest_model->insert($invest_data);
                                
                            }else{
                                $cpa_data['amount'] = 0;
                            }
                            if($cpa_data['is_data_bind'] && $cpa_data['amount']){
                                $cpa_data['is_finish'] = 1; 
                            }
                            $cpa_data['reg_time'] = $val['reg_time'];
                            //插入绑定信息
                            $this->plat_cpa_model->insert_ab($cpa_data);
                        }
                        $import_success++;
                        $import_success_str .= $user['mobile'].',';
                    }else{
                        $import_fail++;
                        $import_fail_str .= $val['mobile_first'].'******'.$val['mobile_end'].',';
                    }
                }
                $mess['status'] = 0;
                $mess['info'] = '成功导入'.$import_success.'条数据['.$import_success_str.'],失败导入'.$import_fail.'条数据['.$import_fail_str.']';
            }else{
                $mess['status'] = 1;
                $mess['error'] = $result['error'];
            }
            
            echo json_encode($mess);die;
        }
    }

    //多融财富
    public function duorongcf(){
        $this->template->assign('plat_en', 'duorongcf');
        $this->template->load('plataward/duorongcf');
    }
    //导入多融财富数据
    public function import_duorongcf_award(){
        $mess = array();
        if($_POST){
            $my_excel = new Mexcel();
            $file_name = trim($_POST['file_url']);
            $name_en = trim($_POST['name_en']);
            $result = $my_excel->excel_to_array($file_name);
            if($result['status'] == 1 ){
                $list_ = $result['data'];
                if(!$list_){
                    $mess['status'] = 1;
                    $mess['error'] = '没有发现任何数据';
                    echo json_encode($mess);die;
                }
                $plat = $this->plat_model->get(array('name_en'=>$name_en,'is_show'=>1));
                if(!$plat){
                    $mess['status'] = 1;
                    $mess['error'] = '平台不存在或已下线';
                    echo json_encode($mess);die;
                }
                $rules = $this->plat_model->get_plat_rules(array('plat_id'=>$plat['id'],'is_show'=>1));
                $list = array();
                $flag_name = 0;//判断平台名称是否正确
                foreach ($list_ as $key=>$val){
                    if($val[0] != $plat['name_ch']){
                        $flag_name = 1;
                        break;
                    }
                    $temp = array();
                    $temp['plat_id'] = $plat['id'];
                    $temp['name_ch'] = $val[0];
                    $temp['mobile_first'] = $val[1];//手机前三位
                    $temp['mobile_end'] = $val[2];//手机后四位
                    $temp['is_data_bind'] = $val[3] == "是" ? 1 : 0;//手机后四位
                    $temp['reg_time'] = strtotime($val[4]);
                    $temp['borrow_nid'] = $val[5];
                    $temp['title'] = $val[6];
                    $temp['url'] = $val[7];
                    $temp['amount'] = $val[8];
                    $temp['period'] = $val[9];
                    $temp['unit'] = $val[10] == '月' ? 2 : 1;
                    $temp['rate'] = $val[11];
                    $temp['invest_time'] = strtotime($val[12]);
                    $temp['is_cpa_orver'] = $val[13] == "是" ? 1 : 0;
                    $temp['is_first_orver'] = $val[14] == "是" ? 1 : 0;
                    $list[] = $temp;
                }
                if($flag_name){
                    $mess['status'] = 1;
                    $mess['error'] = '平台名称解析错误';
                    echo json_encode($mess);die;
                }
                $import_success = 0;//成功导入
                $import_fail = 0;//失败导入
                $import_success_str = '';//成功导入
                $import_fail_str = '';//失败导入
                foreach ($list as $key=>$val){
                    $user = $this->plat_cpa_model->get_users_by_mobile_3_to_3($val['mobile_first'],$val['mobile_end'],$plat['name_en']);
                    if(!$user){
                        $user = $this->users_model->get_users_by_mobile_3_to_3($val['mobile_first'],$val['mobile_end']);
                    }
                    if($user){
                        $cpa = $this->plat_cpa_model->get_ab_user_info(array('user_id'=>$user['user_id'],'plat_en'=>$plat['name_en']));
                        //如果已经绑定cpa
                        if($cpa){
                            if(!$cpa['is_finish']){
                                
                                //cpa绑定信息验证
                                if(!$cpa['is_data_bind'] && $val['is_data_bind']){
                                    $cpa_data = array();
                                    $cpa_data['is_data_bind'] = 1;
                                    $status = $this->plat_cpa_model->update_ab($cpa_data,$cpa['id']);
                                    if($status){
                                        $rule_one = $rules[0];   
                                        $data = array();
                                        $data['user_id'] = $user['user_id'];
                                        $data['plat_id'] = $plat['id'];
                                        $data['plat_en'] = $plat['name_en'];
                                        $data['rule_step'] = 1;
                                        $data['award'] = $rule_one['award'];
                                        $data['exper'] = $rule_one['exper'];
                                        $data['desc'] = $rule_one['rule'];
                                        $data['create_time'] = time();
                                        $this->plat_award_list_model->insert($data);
                                        $plat = $this->plat_model->get(array('id'=>$plat['id']));
                                        //更新平台信息
                                        $plat_data = array();
                                        $plat_data['pay_amount'] = $plat['pay_amount'] - $rule_one['true_amount'];
                                        if($plat_data['pay_amount'] <= 1000000){
                                            $plat_data['is_show'] = 0;
                                        }
                                        $this->plat_model->update_plat($plat_data,$plat['id']);
                                        //添加平台体验日志
                                        $plat_log = array();
                                        $plat_log['user_id'] = $user['user_id'];;
                                        $plat_log['plat_id'] = $plat['id'];
                                        $plat_log['plat_en'] = $plat['name_en'];
                                        $plat_log['plat_balance'] = $plat_data['pay_amount'];
                                        $plat_log['award'] = $data['award'];
                                        $plat_log['true_award'] = $rule_one['true_amount'];
                                        $plat_log['experience'] = $data['exper'];
                                        $plat_log['create_time'] = time();
                                        $this->plat_model->insert_plat_log($plat_log);

                                        //发送奖励通知
                                        $message = "您已经完成【".$plat['name_ch'].'】平台第1步【'.$rule_one['rule']."】的体验,将获得<span class='mail-award'>".$rule_one['award']."溜溜币</span>和<span class='mail-award'>".$rule_one['exper']."积分</span>。感谢您对弹溜溜的支持！";
                                        $message = mail_notice_tpl($this->config_tpl['mail_tpl'], $message);
                                        $this->db->insert("users_award_notice", array('user_id'=>$user['user_id'],'title'=>'奖励通知','content'=>$message,'from'=>'弹溜溜','create_time'=>  time()));
                                    }
                                }
                                
                                
                                
                                //首投信息验证
                                if(!intval($cpa['amount']) && $val['amount']){
                                    $plat = $this->plat_model->get(array('name_en'=>$name_en,'is_show'=>1));
                                    $amount = intval($val['amount']);
                                    foreach ($rules as $k=>$rule){
                                            $step_index = $rule['step'];
                                            $min_amount = $rule['min_amount'];
                                            $max_amount = $rule['max_amount'];
                                            if($amount >= $min_amount && $amount <= $max_amount){//符合要求
                                                $true_amount = $rule['true_amount'];//实际扣除金额
                                                $award_amount = $rule['award'];//奖励金额
                                                $exper_amount = $rule['exper'];//奖励经验
                                                //增加奖励记录
                                                $award_list = array();
                                                $award_list['user_id'] = $user['user_id'];
                                                $award_list['plat_id'] = $plat['id'];
                                                $award_list['plat_en'] = $plat['name_en'];
                                                $award_list['rule_step'] = $step_index;
                                                $award_list['award'] = $award_amount;
                                                $award_list['exper'] = $exper_amount;
                                                $award_list['desc'] = '首次产品体验金额'.  sprintf('%0.2f',$amount).'元';
                                                $award_list['create_time'] = time();
                                                $invest_result = $this->plat_award_list_model->insert($award_list);
                                                if($invest_result){//添加成功
                                                    //更新绑定信息
                                                    $cpa_data = array();
                                                    $cpa_data['amount'] = $amount;
                                                    $cpa_data['is_finish'] = 1;
                                                    $this->plat_cpa_model->update_ab($cpa_data,$cpa['id']);
                                                    //更新平台信息
                                                    $plat_data = array();
                                                    $plat_data['pay_amount'] = $plat['pay_amount'] - $true_amount;
                                                    if($plat_data['pay_amount'] <= 1000000){
                                                        $plat_data['is_show'] = 0;
                                                    }
                                                    $this->plat_model->update_plat($plat_data,$plat['id']);
                                                    //添加平台体验日志
                                                    $plat_log = array();
                                                    $plat_log['user_id'] = $user['user_id'];
                                                    $plat_log['plat_id'] = $plat['id'];
                                                    $plat_log['plat_en'] = $plat['name_en'];
                                                    $plat_log['plat_balance'] = $plat_data['pay_amount'];
                                                    $plat_log['award'] = $award_amount;
                                                    $plat_log['true_award'] = $true_amount;
                                                    $plat_log['experience'] = $exper_amount;
                                                    $plat_log['create_time'] = time();
                                                    $this->plat_model->insert_plat_log($plat_log);
                                                }
                                                break;
                                            }
                                    }
                                    $invest_data = array();
                                    $invest_data['user_id'] = $user['user_id'];
                                    $invest_data['plat_id'] = $plat['id'];
                                    $invest_data['plat_en'] = $plat['name_en'];
                                    $invest_data['order_nid'] = '';
                                    $invest_data['borrow_nid'] = $val['borrow_nid'];
                                    $invest_data['title'] = $val['title'];
                                    $invest_data['url'] = $val['url'];
                                    $invest_data['amount'] = $val['amount'];
                                    $invest_data['invest_time'] = $val['invest_time'];
                                    $invest_data['period'] = $val['period'];
                                    $invest_data['unit'] = $val['unit'];
                                    $invest_data['rate'] = $val['rate'];
                                    $invest_data['create_time'] = time();
                                    $invest_id = $this->plat_invest_model->insert($invest_data);
                                }
                            }
                            
                        }else{//如果未绑定cpa
                            
                            //构造cpa数据
                            $cpa_data = array();
                            $cpa_data['user_id'] = $user['user_id'];
                            $cpa_data['plat_id'] = $plat['id'];
                            $cpa_data['plat_en'] = $plat['name_en'];
                            $cpa_data['usernamep'] = $user['mobile'];
                            
                            //cpa绑定信息验证
                            if($val['is_data_bind']){
                                $cpa_data['is_data_bind'] = 1;
                                $rule_one = $rules[0];   
                                
                                $data = array();
                                $data['user_id'] = $user['user_id'];
                                $data['plat_id'] = $plat['id'];
                                $data['plat_en'] = $plat['name_en'];
                                $data['rule_step'] = 1;
                                $data['award'] = $rule_one['award'];
                                $data['exper'] = $rule_one['exper'];
                                $data['desc'] = $rule_one['rule'];
                                $data['create_time'] = time();
                                $this->plat_award_list_model->insert($data);
                                $plat = $this->plat_model->get(array('id'=>$plat['id']));
                                //更新平台信息
                                $plat_data = array();
                                $plat_data['pay_amount'] = $plat['pay_amount'] - $rule_one['true_amount'];
                                if($plat_data['pay_amount'] <= 1000000){
                                    $plat_data['is_show'] = 0;
                                }
                                $this->plat_model->update_plat($plat_data,$plat['id']);
                                //添加平台体验日志
                                $plat_log = array();
                                $plat_log['user_id'] = $user['user_id'];;
                                $plat_log['plat_id'] = $plat['id'];
                                $plat_log['plat_en'] = $plat['name_en'];
                                $plat_log['plat_balance'] = $plat_data['pay_amount'];
                                $plat_log['award'] = $data['award'];
                                $plat_log['true_award'] = $rule_one['true_amount'];
                                $plat_log['experience'] = $data['exper'];
                                $plat_log['create_time'] = time();
                                $this->plat_model->insert_plat_log($plat_log);

                                //发送奖励通知
                                $message = "您已经完成【".$plat['name_ch'].'】平台第1步【'.$rule_one['rule']."】的体验,将获得<span class='mail-award'>".$rule_one['award']."溜溜币</span>和<span class='mail-award'>".$rule_one['exper']."积分</span>。感谢您对弹溜溜的支持！";
                                $message = mail_notice_tpl($this->config_tpl['mail_tpl'], $message);
                                $this->db->insert("users_award_notice", array('user_id'=>$user['user_id'],'title'=>'奖励通知','content'=>$message,'from'=>'弹溜溜','create_time'=>  time()));
                                
                            }else{
                                $cpa_data['is_data_bind'] = 0;
                            }
                            
                            //判断是否完成首投
                            if($val['amount']){
                                $plat = $this->plat_model->get(array('name_en'=>$name_en,'is_show'=>1));
                                $cpa_data['amount'] = sprintf('%0.2f',$val['amount']);
                                $amount = intval($val['amount']);
                                foreach ($rules as $k=>$rule){
                                    $step_index = $rule['step'];
                                    $min_amount = $rule['min_amount'];
                                    $max_amount = $rule['max_amount'];
                                    if($amount >= $min_amount && $amount <= $max_amount){//符合要求
                                        $true_amount = $rule['true_amount'];//实际扣除金额
                                        $award_amount = $rule['award'];//奖励金额
                                        $exper_amount = $rule['exper'];//奖励经验
                                        //增加奖励记录
                                        $award_list = array();
                                        $award_list['user_id'] = $user['user_id'];
                                        $award_list['plat_id'] = $plat['id'];
                                        $award_list['plat_en'] = $plat['name_en'];
                                        $award_list['rule_step'] = $step_index;
                                        $award_list['award'] = $award_amount;
                                        $award_list['exper'] = $exper_amount;
                                        $award_list['desc'] = '首次产品体验金额'.  sprintf('%0.2f',$amount).'元';
                                        $award_list['create_time'] = time();
                                        $invest_result = $this->plat_award_list_model->insert($award_list);
                                        if($invest_result){//添加成功
                                            $cpa_data['is_data_bind'] = 1;
                                            //更新平台信息
                                            $plat_data = array();
                                            $plat_data['pay_amount'] = $plat['pay_amount'] - $true_amount;
                                            if($plat_data['pay_amount'] <= 1000000){
                                                $plat_data['is_show'] = 0;
                                            }
                                            $this->plat_model->update_plat($plat_data,$plat['id']);
                                            //添加平台体验日志
                                            $plat_log = array();
                                            $plat_log['user_id'] = $user['user_id'];
                                            $plat_log['plat_id'] = $plat['id'];
                                            $plat_log['plat_en'] = $plat['name_en'];
                                            $plat_log['plat_balance'] = $plat_data['pay_amount'];
                                            $plat_log['award'] = $award_amount;
                                            $plat_log['true_award'] = $true_amount;
                                            $plat_log['experience'] = $exper_amount;
                                            $plat_log['create_time'] = time();
                                            $this->plat_model->insert_plat_log($plat_log);

                                            //发送奖励通知
                                            $message = "您已经完成【".$plat['name_ch'].'】平台第'.$step_index.'步【'.$rule['rule']."】的体验,将获得<span class='mail-award'>".$award_amount."溜溜币</span>和<span class='mail-award'>".$exper_amount."积分</span>,赶快去<a href='/user/try_list.html' target='_blank' class='mail-award-a'>立即领取&gt;&gt;</a>吧。感谢您对弹溜溜的支持！";
                                            $message = mail_notice_tpl($this->config_tpl['mail_tpl'], $message);
                                            $this->db->insert("users_award_notice", array('user_id'=>$user['user_id'],'title'=>'奖励通知','content'=>$message,'from'=>'弹溜溜','create_time'=>  time()));

                                        }
                                        break;
                                    }
                                }
                                $invest_data = array();
                                $invest_data['user_id'] = $user['user_id'];
                                $invest_data['plat_id'] = $plat['id'];
                                $invest_data['plat_en'] = $plat['name_en'];
                                $invest_data['order_nid'] = '';
                                $invest_data['borrow_nid'] = $val['borrow_nid'];
                                $invest_data['title'] = $val['title'];
                                $invest_data['url'] = $val['url'];
                                $invest_data['amount'] = $val['amount'];
                                $invest_data['invest_time'] = $val['invest_time'];
                                $invest_data['period'] = $val['period'];
                                $invest_data['unit'] = $val['unit'];
                                $invest_data['rate'] = $val['rate'];
                                $invest_data['create_time'] = time();
                                $invest_id = $this->plat_invest_model->insert($invest_data);
                                
                            }else{
                                $cpa_data['amount'] = 0;
                            }
                            if($cpa_data['is_data_bind'] && $cpa_data['amount']){
                                $cpa_data['is_finish'] = 1; 
                            }
                            $cpa_data['reg_time'] = $val['reg_time'];
                            //插入绑定信息
                            $this->plat_cpa_model->insert_ab($cpa_data);
                        }
                        $import_success++;
                        $import_success_str .= $user['mobile'].',';
                    }else{
                        $import_fail++;
                        $import_fail_str .= $val['mobile_first'].'******'.$val['mobile_end'].',';
                    }
                }
                $mess['status'] = 0;
                $mess['info'] = '成功导入'.$import_success.'条数据['.$import_success_str.'],失败导入'.$import_fail.'条数据['.$import_fail_str.']';
            }else{
                $mess['status'] = 1;
                $mess['error'] = $result['error'];
            }
            
            echo json_encode($mess);die;
        }
    }
    //中融投
    public function topzrt(){
        $this->template->assign('plat_en', 'topzrt');
        $this->template->load('plataward/topzrt');
    }
    
    //导入中融投数据
    public function import_topzrt_award(){
        $mess = array();
        if($_POST){
            $my_excel = new Mexcel();
            $file_name = trim($_POST['file_url']);
            $name_en = trim($_POST['name_en']);
            $result = $my_excel->excel_to_array($file_name);
            if($result['status'] == 1 ){
                $list_ = $result['data'];
                if(!$list_){
                    $mess['status'] = 1;
                    $mess['error'] = '没有发现任何数据';
                    echo json_encode($mess);die;
                }
                $plat = $this->plat_model->get(array('name_en'=>$name_en,'is_show'=>1));
                if(!$plat){
                    $mess['status'] = 1;
                    $mess['error'] = '平台不存在或已下线';
                    echo json_encode($mess);die;
                }
                $rules = $this->plat_model->get_plat_rules(array('plat_id'=>$plat['id'],'is_show'=>1));
                $list = array();
                $flag_name = 0;//判断平台名称是否正确
                foreach ($list_ as $key=>$val){
                    if($val[0] != $plat['name_ch']){
                        $flag_name = 1;
                        break;
                    }
                    $temp = array();
                    $temp['plat_id'] = $plat['id'];
                    $temp['name_ch'] = $val[0];
                    $temp['mobile_first'] = $val[1];//手机前三位
                    $temp['mobile_end'] = $val[2];//手机后四位
                    $temp['is_data_bind'] = $val[3] == "是" ? 1 : 0;//手机后四位
                    $temp['reg_time'] = strtotime($val[4]);
                    $temp['borrow_nid'] = $val[5];
                    $temp['title'] = $val[6];
                    $temp['url'] = $val[7];
                    $temp['amount'] = $val[8];
                    $temp['period'] = $val[9];
                    $temp['unit'] = $val[10] == '月' ? 2 : 1;
                    $temp['rate'] = $val[11];
                    $temp['invest_time'] = strtotime($val[12]);
                    $list[] = $temp;
                }
                if($flag_name){
                    $mess['status'] = 1;
                    $mess['error'] = '平台名称解析错误';
                    echo json_encode($mess);die;
                }
                $import_success = 0;//成功导入
                $import_fail = 0;//失败导入
                $import_success_str = '';//成功导入
                $import_fail_str = '';//失败导入
                foreach ($list as $key=>$val){
                    $user = $this->plat_cpa_model->get_users_by_mobile_3_to_3($val['mobile_first'],$val['mobile_end'],$plat['name_en']);
                    if(!$user){
                        $user = $this->users_model->get_users_by_mobile_3_to_3($val['mobile_first'],$val['mobile_end']);
                    }
                    if($user){
                        $cpa = $this->plat_cpa_model->get_ab_user_info(array('user_id'=>$user['user_id'],'plat_en'=>$plat['name_en']));
                        //如果已经绑定cpa
                        if($cpa){
                            if(!$cpa['is_finish']){
                                
                                //cpa绑定信息验证
                                if(!$cpa['is_data_bind'] && $val['is_data_bind']){
                                    $cpa_data = array();
                                    $cpa_data['is_data_bind'] = 1;
                                    $this->plat_cpa_model->update_ab($cpa_data,$cpa['id']);
                                }
                                //首投信息验证
                                if(!intval($cpa['amount']) && $val['amount']){
                                    $plat = $this->plat_model->get(array('name_en'=>$name_en,'is_show'=>1));
                                    $amount = intval($val['amount']);
                                    foreach ($rules as $k=>$rule){
                                            $step_index = $rule['step'];
                                            $min_amount = $rule['min_amount'];
                                            $max_amount = $rule['max_amount'];
                                            if($amount >= $min_amount && $amount <= $max_amount){//符合要求
                                                $true_amount = $rule['true_amount'];//实际扣除金额
                                                $award_amount = $rule['award'];//奖励金额
                                                $exper_amount = $rule['exper'];//奖励经验
                                                //增加奖励记录
                                                $award_list = array();
                                                $award_list['user_id'] = $user['user_id'];
                                                $award_list['plat_id'] = $plat['id'];
                                                $award_list['plat_en'] = $plat['name_en'];
                                                $award_list['rule_step'] = $step_index;
                                                $award_list['award'] = $award_amount;
                                                $award_list['exper'] = $exper_amount;
                                                $award_list['desc'] = '首次产品体验金额'.  sprintf('%0.2f',$amount).'元';
                                                $award_list['create_time'] = time();
                                                $invest_result = $this->plat_award_list_model->insert($award_list);
                                                if($invest_result){//添加成功
                                                    //更新绑定信息
                                                    $cpa_data = array();
                                                    $cpa_data['amount'] = $amount;
                                                    $cpa_data['is_finish'] = 1;
                                                    $this->plat_cpa_model->update_ab($cpa_data,$cpa['id']);
                                                    //更新平台信息
                                                    $plat_data = array();
                                                    $plat_data['pay_amount'] = $plat['pay_amount'] - $true_amount;
                                                    if($plat_data['pay_amount'] <= 2000000){
                                                        $plat_data['is_show'] = 0;
                                                    }
                                                    $this->plat_model->update_plat($plat_data,$plat['id']);
                                                    //添加平台体验日志
                                                    $plat_log = array();
                                                    $plat_log['user_id'] = $user['user_id'];
                                                    $plat_log['plat_id'] = $plat['id'];
                                                    $plat_log['plat_en'] = $plat['name_en'];
                                                    $plat_log['plat_balance'] = $plat_data['pay_amount'];
                                                    $plat_log['award'] = $award_amount;
                                                    $plat_log['true_award'] = $true_amount;
                                                    $plat_log['experience'] = $exper_amount;
                                                    $plat_log['create_time'] = time();
                                                    $this->plat_model->insert_plat_log($plat_log);
                                                }
                                                break;
                                            }
                                    }
                                    $invest_data = array();
                                    $invest_data['user_id'] = $user['user_id'];
                                    $invest_data['plat_id'] = $plat['id'];
                                    $invest_data['plat_en'] = $plat['name_en'];
                                    $invest_data['order_nid'] = '';
                                    $invest_data['borrow_nid'] = $val['borrow_nid'];
                                    $invest_data['title'] = $val['title'];
                                    $invest_data['url'] = $val['url'];
                                    $invest_data['amount'] = $val['amount'];
                                    $invest_data['invest_time'] = $val['invest_time'];
                                    $invest_data['period'] = $val['period'];
                                    $invest_data['unit'] = $val['unit'];
                                    $invest_data['rate'] = $val['rate'];
                                    $invest_data['create_time'] = time();
                                    $invest_id = $this->plat_invest_model->insert($invest_data);
                                }
                            }
                            
                        }else{//如果未绑定cpa
                            
                            //构造cpa数据
                            $cpa_data = array();
                            $cpa_data['user_id'] = $user['user_id'];
                            $cpa_data['plat_id'] = $plat['id'];
                            $cpa_data['plat_en'] = $plat['name_en'];
                            $cpa_data['usernamep'] = $user['mobile'];
                            
                            //cpa绑定信息验证
                            if($val['is_data_bind']){
                                $cpa_data['is_data_bind'] = 1;
                            }else{
                                $cpa_data['is_data_bind'] = 0;
                            }
                            
                            //判断是否完成首投
                            if($val['amount']){
                                $plat = $this->plat_model->get(array('name_en'=>$name_en,'is_show'=>1));
                                $cpa_data['amount'] = sprintf('%0.2f',$val['amount']);
                                $amount = intval($val['amount']);
                                foreach ($rules as $k=>$rule){
                                    $step_index = $rule['step'];
                                    $min_amount = $rule['min_amount'];
                                    $max_amount = $rule['max_amount'];
                                    if($amount >= $min_amount && $amount <= $max_amount){//符合要求
                                        $true_amount = $rule['true_amount'];//实际扣除金额
                                        $award_amount = $rule['award'];//奖励金额
                                        $exper_amount = $rule['exper'];//奖励经验
                                        //增加奖励记录
                                        $award_list = array();
                                        $award_list['user_id'] = $user['user_id'];
                                        $award_list['plat_id'] = $plat['id'];
                                        $award_list['plat_en'] = $plat['name_en'];
                                        $award_list['rule_step'] = $step_index;
                                        $award_list['award'] = $award_amount;
                                        $award_list['exper'] = $exper_amount;
                                        $award_list['desc'] = '首次产品体验金额'.  sprintf('%0.2f',$amount).'元';
                                        $award_list['create_time'] = time();
                                        $invest_result = $this->plat_award_list_model->insert($award_list);
                                        if($invest_result){//添加成功
                                            $cpa_data['is_data_bind'] = 1;
                                            //更新平台信息
                                            $plat_data = array();
                                            $plat_data['pay_amount'] = $plat['pay_amount'] - $true_amount;
                                            if($plat_data['pay_amount'] <= 2000000){
                                                $plat_data['is_show'] = 0;
                                            }
                                            $this->plat_model->update_plat($plat_data,$plat['id']);
                                            //添加平台体验日志
                                            $plat_log = array();
                                            $plat_log['user_id'] = $user['user_id'];
                                            $plat_log['plat_id'] = $plat['id'];
                                            $plat_log['plat_en'] = $plat['name_en'];
                                            $plat_log['plat_balance'] = $plat_data['pay_amount'];
                                            $plat_log['award'] = $award_amount;
                                            $plat_log['true_award'] = $true_amount;
                                            $plat_log['experience'] = $exper_amount;
                                            $plat_log['create_time'] = time();
                                            $this->plat_model->insert_plat_log($plat_log);

                                            //发送奖励通知
                                            $message = "您已经完成【".$plat['name_ch'].'】平台第'.$step_index.'步【'.$rule['rule']."】的体验,将获得<span class='mail-award'>".$award_amount."溜溜币</span>和<span class='mail-award'>".$exper_amount."积分</span>,赶快去<a href='/user/try_list.html' target='_blank' class='mail-award-a'>立即领取&gt;&gt;</a>吧。感谢您对弹溜溜的支持！";
                                            $message = mail_notice_tpl($this->config_tpl['mail_tpl'], $message);
                                            $this->db->insert("users_award_notice", array('user_id'=>$user['user_id'],'title'=>'奖励通知','content'=>$message,'from'=>'弹溜溜','create_time'=>  time()));

                                        }
                                        break;
                                    }
                                }
                                $invest_data = array();
                                $invest_data['user_id'] = $user['user_id'];
                                $invest_data['plat_id'] = $plat['id'];
                                $invest_data['plat_en'] = $plat['name_en'];
                                $invest_data['order_nid'] = '';
                                $invest_data['borrow_nid'] = $val['borrow_nid'];
                                $invest_data['title'] = $val['title'];
                                $invest_data['url'] = $val['url'];
                                $invest_data['amount'] = $val['amount'];
                                $invest_data['invest_time'] = $val['invest_time'];
                                $invest_data['period'] = $val['period'];
                                $invest_data['unit'] = $val['unit'];
                                $invest_data['rate'] = $val['rate'];
                                $invest_data['create_time'] = time();
                                $invest_id = $this->plat_invest_model->insert($invest_data);
                                
                            }else{
                                $cpa_data['amount'] = 0;
                            }
                            if($cpa_data['is_data_bind'] && $cpa_data['amount']){
                                $cpa_data['is_finish'] = 1; 
                            }
                            $cpa_data['reg_time'] = $val['reg_time'];
                            //插入绑定信息
                            $this->plat_cpa_model->insert_ab($cpa_data);
                        }
                        $import_success++;
                        $import_success_str .= $user['mobile'].',';
                    }else{
                        $import_fail++;
                        $import_fail_str .= $val['mobile_first'].'******'.$val['mobile_end'].',';
                    }
                }
                $mess['status'] = 0;
                $mess['info'] = '成功导入'.$import_success.'条数据['.$import_success_str.'],失败导入'.$import_fail.'条数据['.$import_fail_str.']';
            }else{
                $mess['status'] = 1;
                $mess['error'] = $result['error'];
            }
            
            echo json_encode($mess);die;
        }
    }
    
     //绿化贷
    public function lvhuadai(){
        $this->template->assign('plat_en', 'lvhuadai');
        $this->template->load('plataward/lvhuadai');
    }
    //绿化贷数据导入
    public function import_lvhuadai_award(){
        $mess = array();
        if($_POST){
            $my_excel = new Mexcel();
            $file_name = trim($_POST['file_url']);
            $name_en = trim($_POST['name_en']);
            $result = $my_excel->excel_to_array($file_name);
            if($result['status'] == 1 ){
                $list_ = $result['data'];
                if(!$list_){
                    $mess['status'] = 1;
                    $mess['error'] = '没有发现任何数据';
                    echo json_encode($mess);die;
                }
                $plat = $this->plat_model->get(array('name_en'=>$name_en));
                if(!$plat){
                    $mess['status'] = 1;
                    $mess['error'] = '平台不存在或已下线';
                    echo json_encode($mess);die;
                }
                $list = array();
                $flag_name = 0;//判断平台名称是否正确
                foreach ($list_ as $key=>$val){
                    if($val[0] != $plat['name_ch']){
                        $flag_name = 1;
                        break;
                    }
                    $temp = array();
                    $temp['plat_id'] = $plat['id'];
                    $temp['name_ch'] = $val[0];
                    $temp['mobile'] = $val[1];//手机前三位
                    $temp['is_data_bind'] = $val[2] == "是" ? 1 : 0;
                    $temp['truename'] = $val[3];
                    $temp['reg_time'] = strtotime($val[4]);
                    $temp['amount'] = $val[5];
                    $temp['invest_time'] = $val[6] ? strtotime($val[6]): 0 ;
                    $list[] = $temp;
                }
                if($flag_name){
                    $mess['status'] = 1;
                    $mess['error'] = '平台名称解析错误';
                    echo json_encode($mess);die;
                }
                $import_success = 0;//成功导入
                $import_fail = 0;//失败导入
                $import_success_str = '';//成功导入
                $import_fail_str = '';//失败导入
                foreach ($list as $key=>$val){
                    $user = $this->users_model->get_user_by_mobile($val['mobile']);
                    if($user){
                        $cpa = $this->plat_cpa_model->get_ab_user_info(array('user_id'=>$user['user_id'],'plat_en'=>$plat['name_en']));
                        if(!$cpa){//如果未绑定cpa
                            
                            //构造cpa数据
                            $cpa_data = array();
                            $cpa_data['user_id'] = $user['user_id'];
                            $cpa_data['plat_id'] = $plat['id'];
                            $cpa_data['plat_en'] = $plat['name_en'];
                            $cpa_data['usernamep'] = $user['mobile'];
                            //cpa绑定信息验证
                            if($val['is_data_bind']){
                                $cpa_data['is_data_bind'] = 1;
                            }else{
                                $cpa_data['is_data_bind'] = 0;
                            }
                            if($val['amount']){
                                $cpa_data['amount'] = sprintf('%0.2f',$val['amount']);
                                $cpa_data['first_invest_time'] = $val['invest_time'];
                            }else{
                                $cpa_data['amount'] = 0;
                                $cpa_data['first_invest_time'] = 0;
                            }
                            
                            if($cpa_data['is_data_bind'] && $cpa_data['amount']){
                                $cpa_data['is_finish'] = 1; 
                            }
                            $cpa_data['reg_time'] = $val['reg_time'];
                            //插入绑定信息
                            $this->plat_cpa_model->insert_ab($cpa_data);
                        }
                        $import_success++;
                        $import_success_str .= $user['mobile'].',';
                    }else{
                        $import_fail++;
                        $import_fail_str .= $val['mobile'].',';
                    }
                }
                $mess['status'] = 0;
                $mess['info'] = '成功导入'.$import_success.'条数据['.$import_success_str.'],失败导入'.$import_fail.'条数据['.$import_fail_str.']';
            }else{
                $mess['status'] = 1;
                $mess['error'] = $result['error'];
            }
            echo json_encode($mess);die;
        }
    }
    
    //人众金服
    public function rzjf(){
        $this->template->assign('plat_en', '51rz');
        $this->template->load('plataward/51rz');
    }
    
    //人众金服数据导入
    public function import_51rz_award(){
        $mess = array();
        if($_POST){
            $my_excel = new Mexcel();
            $file_name = trim($_POST['file_url']);
            $name_en = trim($_POST['name_en']);
            $result = $my_excel->excel_to_array($file_name);
            if($result['status'] == 1 ){
                $list_ = $result['data'];
                if(!$list_){
                    $mess['status'] = 1;
                    $mess['error'] = '没有发现任何数据';
                    echo json_encode($mess);die;
                }
                $plat = $this->plat_model->get(array('name_en'=>$name_en,'is_show'=>1));
                if(!$plat){
                    $mess['status'] = 1;
                    $mess['error'] = '平台不存在或已下线';
                    echo json_encode($mess);die;
                }
                $rules = $this->plat_model->get_plat_rules(array('plat_id'=>$plat['id'],'is_show'=>1));
                $list = array();
                $flag_name = 0;//判断平台名称是否正确
                foreach ($list_ as $key=>$val){
                    if($val[0] != $plat['name_ch']){
                        $flag_name = 1;
                        break;
                    }
                    $temp = array();
                    $temp['plat_id'] = $plat['id'];
                    $temp['name_ch'] = $val[0];
                    $temp['mobile'] = $val[1];//手机号
                    $temp['is_data_bind'] = $val[2] == "是" ? 1 : 0;//手机后四位
                    $temp['reg_time'] = strtotime($val[3]);
                    $temp['borrow_nid'] = $val[4];
                    $temp['title'] = $val[5];
                    $temp['url'] = $val[6];
                    $temp['amount'] = $val[7];
                    $temp['period'] = $val[8];
                    $temp['unit'] = $val[9] == '月' ? 2 : 1;
                    $temp['rate'] = $val[10];
                    $temp['invest_time'] = strtotime($val[11]);
                    $list[] = $temp;
                }
                if($flag_name){
                    $mess['status'] = 1;
                    $mess['error'] = '平台名称解析错误';
                    echo json_encode($mess);die;
                }
                $import_success = 0;//成功导入
                $import_fail = 0;//失败导入
                $import_success_str = '';//成功导入
                $import_fail_str = '';//失败导入
                foreach ($list as $key=>$val){
                    $user = $this->plat_cpa_model->get_ab_user_info(array('usernamep'=>$val['mobile'],'plat_en'=>$plat['name_en']));
                    if(!$user){
                        $user = $this->users_model->get_user_by_mobile($val['mobile']);
                    }
                    if($user){
                        $cpa = $this->plat_cpa_model->get_ab_user_info(array('user_id'=>$user['user_id'],'plat_en'=>$plat['name_en']));
                        //如果已经绑定cpa
                        if($cpa){
                            if(!$cpa['is_finish']){
                                //cpa绑定信息验证
                                if(!$cpa['is_data_bind'] && $val['is_data_bind']){
                                    $cpa_data = array();
                                    $cpa_data['is_data_bind'] = 1;
                                    $this->plat_cpa_model->update_ab($cpa_data,$cpa['id']);
                                }
                                //首投信息验证
                                if(!intval($cpa['amount']) && $val['amount']){
                                    $plat = $this->plat_model->get(array('name_en'=>$name_en,'is_show'=>1));
                                    $amount = intval($val['amount']);
                                    foreach ($rules as $k=>$rule){
                                            $step_index = $rule['step'];
                                            $min_amount = $rule['min_amount'];
                                            $max_amount = $rule['max_amount'];
                                            if($amount >= $min_amount && $amount <= $max_amount){//符合要求
                                                $true_amount = $rule['true_amount'];//实际扣除金额
                                                $award_amount = $rule['award'];//奖励金额
                                                $exper_amount = $rule['exper'];//奖励经验
                                                //增加奖励记录
                                                $award_list = array();
                                                $award_list['user_id'] = $user['user_id'];
                                                $award_list['plat_id'] = $plat['id'];
                                                $award_list['plat_en'] = $plat['name_en'];
                                                $award_list['rule_step'] = $step_index;
                                                $award_list['award'] = $award_amount;
                                                $award_list['exper'] = $exper_amount;
                                                $award_list['desc'] = '首次产品体验金额'.  sprintf('%0.2f',$amount).'元';
                                                $award_list['create_time'] = time();
                                                $invest_result = $this->plat_award_list_model->insert($award_list);
                                                if($invest_result){//添加成功
                                                    //更新绑定信息
                                                    $cpa_data = array();
                                                    $cpa_data['amount'] = $amount;
                                                    $cpa_data['is_finish'] = 1;
                                                    $this->plat_cpa_model->update_ab($cpa_data,$cpa['id']);
                                                    //更新平台信息
                                                    $plat_data = array();
                                                    $plat_data['pay_amount'] = $plat['pay_amount'] - $true_amount;
                                                    if($plat_data['pay_amount'] <= 2000000){
                                                        $plat_data['is_show'] = 0;
                                                    }
                                                    $this->plat_model->update_plat($plat_data,$plat['id']);
                                                    //添加平台体验日志
                                                    $plat_log = array();
                                                    $plat_log['user_id'] = $user['user_id'];
                                                    $plat_log['plat_id'] = $plat['id'];
                                                    $plat_log['plat_en'] = $plat['name_en'];
                                                    $plat_log['plat_balance'] = $plat_data['pay_amount'];
                                                    $plat_log['award'] = $award_amount;
                                                    $plat_log['true_award'] = $true_amount;
                                                    $plat_log['experience'] = $exper_amount;
                                                    $plat_log['create_time'] = time();
                                                    $this->plat_model->insert_plat_log($plat_log);
                                                }
                                                break;
                                            }
                                    }
                                    $invest_data = array();
                                    $invest_data['user_id'] = $user['user_id'];
                                    $invest_data['plat_id'] = $plat['id'];
                                    $invest_data['plat_en'] = $plat['name_en'];
                                    $invest_data['order_nid'] = '';
                                    $invest_data['borrow_nid'] = $val['borrow_nid'];
                                    $invest_data['title'] = $val['title'];
                                    $invest_data['url'] = $val['url'];
                                    $invest_data['amount'] = $val['amount'];
                                    $invest_data['invest_time'] = $val['invest_time'];
                                    $invest_data['period'] = $val['period'];
                                    $invest_data['unit'] = $val['unit'];
                                    $invest_data['rate'] = $val['rate'];
                                    $invest_data['create_time'] = time();
                                    $invest_id = $this->plat_invest_model->insert($invest_data);
                                }
                            }
                            
                        }else{//如果未绑定cpa
                            
                            //构造cpa数据
                            $cpa_data = array();
                            $cpa_data['user_id'] = $user['user_id'];
                            $cpa_data['plat_id'] = $plat['id'];
                            $cpa_data['plat_en'] = $plat['name_en'];
                            $cpa_data['usernamep'] = $user['mobile'];
                            
                            //cpa绑定信息验证
                            if($val['is_data_bind']){
                                $cpa_data['is_data_bind'] = 1;
                            }else{
                                $cpa_data['is_data_bind'] = 0;
                            }
                            
                            //判断是否完成首投
                            if($val['amount']){
                                $plat = $this->plat_model->get(array('name_en'=>$name_en,'is_show'=>1));
                                $cpa_data['amount'] = sprintf('%0.2f',$val['amount']);
                                $amount = intval($val['amount']);
                                foreach ($rules as $k=>$rule){
                                    $step_index = $rule['step'];
                                    $min_amount = $rule['min_amount'];
                                    $max_amount = $rule['max_amount'];
                                    if($amount >= $min_amount && $amount <= $max_amount){//符合要求
                                        $true_amount = $rule['true_amount'];//实际扣除金额
                                        $award_amount = $rule['award'];//奖励金额
                                        $exper_amount = $rule['exper'];//奖励经验
                                        //增加奖励记录
                                        $award_list = array();
                                        $award_list['user_id'] = $user['user_id'];
                                        $award_list['plat_id'] = $plat['id'];
                                        $award_list['plat_en'] = $plat['name_en'];
                                        $award_list['rule_step'] = $step_index;
                                        $award_list['award'] = $award_amount;
                                        $award_list['exper'] = $exper_amount;
                                        $award_list['desc'] = '首次产品体验金额'.  sprintf('%0.2f',$amount).'元';
                                        $award_list['create_time'] = time();
                                        $invest_result = $this->plat_award_list_model->insert($award_list);
                                        if($invest_result){//添加成功
                                            $cpa_data['is_data_bind'] = 1;
                                            //更新平台信息
                                            $plat_data = array();
                                            $plat_data['pay_amount'] = $plat['pay_amount'] - $true_amount;
                                            if($plat_data['pay_amount'] <= 2000000){
                                                $plat_data['is_show'] = 0;
                                            }
                                            $this->plat_model->update_plat($plat_data,$plat['id']);
                                            //添加平台体验日志
                                            $plat_log = array();
                                            $plat_log['user_id'] = $user['user_id'];
                                            $plat_log['plat_id'] = $plat['id'];
                                            $plat_log['plat_en'] = $plat['name_en'];
                                            $plat_log['plat_balance'] = $plat_data['pay_amount'];
                                            $plat_log['award'] = $award_amount;
                                            $plat_log['true_award'] = $true_amount;
                                            $plat_log['experience'] = $exper_amount;
                                            $plat_log['create_time'] = time();
                                            $this->plat_model->insert_plat_log($plat_log);

                                            //发送奖励通知
                                            $message = "您已经完成【".$plat['name_ch'].'】平台第'.$step_index.'步【'.$rule['rule']."】的体验,将获得<span class='mail-award'>".$award_amount."溜溜币</span>和<span class='mail-award'>".$exper_amount."积分</span>,赶快去<a href='/user/try_list.html' target='_blank' class='mail-award-a'>立即领取&gt;&gt;</a>吧。感谢您对弹溜溜的支持！";
                                            $message = mail_notice_tpl($this->config_tpl['mail_tpl'], $message);
                                            $this->db->insert("users_award_notice", array('user_id'=>$user['user_id'],'title'=>'奖励通知','content'=>$message,'from'=>'弹溜溜','create_time'=>  time()));

                                        }
                                        break;
                                    }
                                }
                                $invest_data = array();
                                $invest_data['user_id'] = $user['user_id'];
                                $invest_data['plat_id'] = $plat['id'];
                                $invest_data['plat_en'] = $plat['name_en'];
                                $invest_data['order_nid'] = '';
                                $invest_data['borrow_nid'] = $val['borrow_nid'];
                                $invest_data['title'] = $val['title'];
                                $invest_data['url'] = $val['url'];
                                $invest_data['amount'] = $val['amount'];
                                $invest_data['invest_time'] = $val['invest_time'];
                                $invest_data['period'] = $val['period'];
                                $invest_data['unit'] = $val['unit'];
                                $invest_data['rate'] = $val['rate'];
                                $invest_data['create_time'] = time();
                                $invest_id = $this->plat_invest_model->insert($invest_data);
                                
                            }else{
                                $cpa_data['amount'] = 0;
                            }
                            if($cpa_data['is_data_bind'] && $cpa_data['amount']){
                                $cpa_data['is_finish'] = 1; 
                            }
                            $cpa_data['reg_time'] = $val['reg_time'];
                            //插入绑定信息
                            $this->plat_cpa_model->insert_ab($cpa_data);
                        }
                        $import_success++;
                        $import_success_str .= $val['mobile'].',';
                    }else{
                        $import_fail++;
                        $import_fail_str .= $val['mobile'].',';
                    }
                }
                $mess['status'] = 0;
                $mess['info'] = '成功导入'.$import_success.'条数据['.$import_success_str.'],失败导入'.$import_fail.'条数据['.$import_fail_str.']';
            }else{
                $mess['status'] = 1;
                $mess['error'] = $result['error'];
            }
            
            echo json_encode($mess);die;
        }
    }
    
}
